package com.api.gbhg.zengping.action;

import com.api.gbhg.zengping.common.CommonDao;
import com.api.gbhg.zengping.dao.BudgeSourceDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: gbhg-oa
 * @author: Mr.zeng
 * @create: 2021-01-14 10:30
 *
 * 扣减预算来源
 **/

public class DeductionBudget implements Action {
    public static final Logger LOGGER = LoggerFactory.getLogger("gbhg");
    private final BudgeSourceDao budgeSourceDao=new BudgeSourceDao();
    @Override
    public String execute(RequestInfo requestInfo) {
        LOGGER.info("===================进入扣减预算来源Action==========================");
        String requestid = requestInfo.getRequestid();
        //得到明细表名
        DetailTable[] detailTables = CommonDao.getDetailTables(requestInfo);
        //当前流程表名-类似 formtable_main_22
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = CommonDao.selectByRequestId(new RecordSet(), billTableName, requestid);
        rs.next();
        String mainid = rs.getString("id");
        RecordSet recordSet = CommonDao.selectByDetailTable(detailTables[4].getTableDBName(), mainid);
        while (recordSet.next()){
            //扣减来源表金额
            Map<String,Object> map=new HashMap<>();
            map.put("requestId",requestid);
            //预算来源
            map.put("ysly",recordSet.getString("ysly"));
            double hpje = recordSet.getDouble("hpje");
            //扣减金额
            map.put("deduction",hpje);
            boolean b3 = budgeSourceDao.modifyBudgeSource(map);
            if(!b3){
                LOGGER.info("============================扣减预算来源出现错误，修改审批状态错误======================================");
                requestInfo.getRequestManager().setMessagecontent("归档后扣减金额出现错误，修改审批状态错误");
                requestInfo.getRequestManager().setMessageid("1");
                return FAILURE_AND_CONTINUE;
            }
        }
        return SUCCESS;
    }

}
